import { isFunction, isUndefined } from "@tarojs/shared";
import Taro from "@tarojs/taro";
import { shBaseUrl } from "./urls";
import useUserInfoStore from "../store/user"
import dayjs from "dayjs";
const state = useUserInfoStore()
// 请求拦截器
const interceptor = function (chain) {

  var requestParams = chain.requestParams;
  var p;
  var res: any = new Promise(function (resolve, reject) {
    p = chain.proceed(requestParams)
    p.then(function (res) {
      if (res.statusCode === 200) {
        if(res.data.code === 401){
          Taro.showToast({
            icon: 'error',
            title: '登录状态过期',
            success:()=>{
              Taro.removeStorageSync('userToken')
              Taro.removeStorageSync('openid')
              Taro.removeStorageSync('userInfo')
              state.removeToken()
              state.removeOpenid()
              state.removeUserinfo()
              Taro.redirectTo({
                url: `/pages/login/index`,
              })
            }
          })
          reject(Error('登录状态已过期'))
        }
        else if(res.data.code === 500){
          if(res.data.msg){
            Taro.showToast({
              icon: 'none',
              title: res.data.msg
            })
          }
          else {
            Taro.showToast({
              icon: 'error',
              title: '接口异常'
            })
          }
          resolve(res)
        }
        else {
          resolve(res)
        }
      } else {
        Taro.showToast({
          icon: 'error',
          title: res.errMsg
        })
        reject(Error('错误'))
      }
    }).catch(function (err) {
      reject(err);
    });
  });
  if (!isUndefined(p) && isFunction(p.abort)) res.abort = p.abort;
  return res;
}

Taro.addInterceptor(Taro.interceptors.timeoutInterceptor)
Taro.addInterceptor(interceptor)


export async function $upload(urlKey: string, data: any){
  let url
  if (urlKey.includes('http://') || urlKey.includes('https://')) {
    url = urlKey
  } else {
    url = shBaseUrl + urlKey
  }
  // const boundary = `----FooBar${new Date().getTime()}`;
  // const formData = createFormData(data, boundary);
  return (await Taro.uploadFile({
    url: url + (data.billNo?`?billNo=${data.billNo}`:''),
    filePath: data.filePath,
    header: {
      "Content-Type": "multipart/form-data",
      Authorization: state.token,
    },
    name: 'file',
  }))
}

export async function $upload2(urlKey: string, dataKey: string, data: any){
  let url
  if (urlKey.includes('http://') || urlKey.includes('https://')) {
    url = urlKey
  } else {
    url = shBaseUrl + urlKey
  }
  return (await Taro.uploadFile({
    url: url,
    filePath: data,
    header: {
      "Content-Type": "multipart/form-data",
      Authorization: state.token,
    },
    name: dataKey,
  }))
}

export async function $post<TResp extends string | Record<string, any> | ArrayBuffer, TData = any>(urlKey: string, data: TData): Promise<Taro.request.SuccessCallbackResult<TResp>> {
  let url
  if (urlKey.includes('http://') || urlKey.includes('https://')) {
    url = urlKey
  } else {
    url = shBaseUrl + urlKey
  }
  return (await Taro.request({
    method: 'POST',
    header: { 'content-type': 'application/json', 'Authorization': state.token, },
    url: url,
    data: data
  }))
}
export async function $get<TResp extends string | Record<string, any> | ArrayBuffer, TData = any>(urlKey: string, data?: TData,option: { noShowLoading?: boolean } = {}): Promise<Taro.request.SuccessCallbackResult<TResp>> {
  let url
  if (urlKey.includes('http://') || urlKey.includes('https://')) {
    url = urlKey
  } else {
    url = shBaseUrl + urlKey
  }
  return (await Taro.request({
    method: 'GET',
    header: { 'content-type': 'application/json', 'Authorization': state.token, },
    url: url,
    data: data,
    noShowLoading: option.noShowLoading,
  }))
}
export async function $put<TResp extends string | Record<string, any> | ArrayBuffer, TData = any>(urlKey: string, data?: TData): Promise<Taro.request.SuccessCallbackResult<TResp>> {
  let url
  if (urlKey.includes('http://') || urlKey.includes('https://')) {
    url = urlKey
  } else {
    url = shBaseUrl + urlKey
  }
  return (await Taro.request({
    method: 'PUT',
    header: { 'content-type': 'application/json', 'Authorization': state.token, },
    url: url,
    data: data
  }))
}
export async function $del<TResp extends string | Record<string, any> | ArrayBuffer, TData = any>(urlKey: string, data: TData): Promise<Taro.request.SuccessCallbackResult<TResp>> {
  let url
  if (urlKey.includes('http://') || urlKey.includes('https://')) {
    url = urlKey
  } else {
    url = shBaseUrl + urlKey
  }
  return (await Taro.request({
    method: 'DELETE',
    header: { 'content-type': 'application/json', 'Authorization': state.token, },
    url: url,
    data: data
  }))
}
//地址簿列表
interface AddressParams {
  customerCode: string
}
interface DataParams {
  /**
   * 省市区详细地址 用 / 分割
   */
  preAdd: string,
  /**
   * 全部地址
   */
  preAddress: string,
  /**
   * 手机号
   */
  preMobile: string,
  /**
   * 姓名
   */
  preName: string,
  /**
   * 手机号
   */
  preTel: string,
  /**
   * 收发货人  1发货人 2收货人  3通用
   */
  infoType: string,
  /**
   * 是否默认  1 是  0否
   */
  isDefault: string,
  /**
   * id
   */
  btcaId: string,
}
export function getAddressLists(data: AddressParams): Promise<DataParams[]> {
  return new Promise<DataParams[]>(async (resolve, reject) => {
    const res = await $get<{ code: number, data: DataParams[], msg: string }>('/selfmachine-system/workerAddress/list', data)
    if (res.data.code == 200) {
      resolve(res.data.data)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}

export function getAddressListSend(data: AddressParams): Promise<DataParams[]> {
  return new Promise<DataParams[]>(async (resolve, reject) => {
    const res = await $get<{ code: number, data: DataParams[], msg: string }>('/selfmachine-system/workerAddress/list/1', data)
    if (res.data.code == 200) {
      resolve(res.data.data)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}

export function getAddressListReceive(data: AddressParams): Promise<DataParams[]> {
  return new Promise<DataParams[]>(async (resolve, reject) => {
    const res = await $get<{ code: number, data: DataParams[], msg: string }>('/selfmachine-system/workerAddress/list/2', data)
    if (res.data.code == 200) {
      resolve(res.data.data)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}

export function getMyAddressList(data: any): Promise<DataParams[]> {
  return new Promise<DataParams[]>(async (resolve, reject) => {
    const res = await $post<{ code: number, data: DataParams[], msg: string }>('/selfmachine-system/addressBook/getByUserId', data)
    if (res.data.code == 200) {
      resolve(res.data.data)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}


interface AddParams {
  preAdd: string,
  preAddress: string,
  preAddressNote: null,
  preArea: null,
  preMobile: null,
  preName: string,
  preTel: string,
  infoType: string,
  isDefault: string,
  email: null,
  customerCode: string,
  btcaId?: string,
  opFlag?: string,
  latitude?: string,
  longitude?: string,
}
//地址簿新增
export function addressBookUpdate(data: AddParams): Promise<number> {
  return new Promise<number>(async (resolve, reject) => {
    const res = await $post<{ code: number, data: DataParams[], msg: string }>('/selfmachine-system/addressBook/addOrUpdate', data)
    if (res.data.code == 200) {
      resolve(res.data.code)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}

//地址簿删除
interface DelParams {
  btcaId: string
}
export function addressBookDelete(data: DelParams): Promise<number> {
  return new Promise<number>(async (resolve, reject) => {
    const res = await $del<{ code: number, data: DataParams[], msg: string }>('/selfmachine-system/addressBook/delete', data)
    if (res.data.code == 200) {
      resolve(res.data.code)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}


interface PublicParms {
  /**
   *  1 货物名称 2货物类型  3货物包装
   */
  ssopSign?: number,
}
//通用类   货物类型名字包装
export function publicInfo(data: PublicParms): Promise<any> {
  return new Promise<any>(async (resolve, reject) => {
    const res = await $get<{ code: number, data: any, msg: string }>('/selfmachine-system/order/publicInfo', data)

    if (res.data.code == 200) {
      resolve(res.data.data)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}

//签单单位 获取
export function getSignUnit(data: AddressParams): Promise<any> {
  return new Promise<any>(async (resolve, reject) => {
    const res = await $get<{ code: number, rows: any, msg: string }>('/selfmachine-system/order/getSignUnit', data)

    if (res.data.code == 200) {
      resolve(res.data.rows || [])
    } else 
      resolve([])
  })
}

//银行卡列表查询
export interface AccountListParam {
  /**
   *  用户id
   */
  customerCode: string,
  /**
   *  开户名字
   */
  accountName: string,
  /**
   *  电话
   */
  phone: string,
  /**
   *  银行卡号
   */
  accountNo: string,
}
export interface AccountDateParams {
  /**
   * 名字
   */
  accountName: string,
  /**
   * 地址
   */
  accountRegion: string,
  /**
   * 银行名字
   */
  bankName: string,
  /**
   * 银行卡号
   */
  bankNo: string,
  /**
   * 支行名字
   */
  bankSubName: string,
  /**
   * id
   */
  btcaId: string,
  /**
   * 手机号
   */
  phone: string,
  /**
   * 类型
   */
  type: string
}
export function accountList(data: AccountListParam): Promise<AccountDateParams[]> {
  return new Promise<AccountDateParams[]>(async (resolve, reject) => {
    const res = await $post<{ code: number, data: AccountDateParams[], msg: string }>('/selfmachine-system/account/list', data)
    console.log(res, '银行卡');

    if (res.data.code == 200) {
      resolve(res.data.data)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}

//银行卡增改
export function accountAddOrUpdate(data: AccountListParam): Promise<AccountDateParams[]> {
  return new Promise<AccountDateParams[]>(async (resolve, reject) => {
    const res = await $post<{ code: number, data: AccountDateParams[], msg: string }>('/selfmachine-system/account/addOrUpdate', data)
    console.log(res, '银行卡');

    if (res.data.code == 200) {
      resolve(res.data.data)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}
export interface RecommandParam {
  /**
   *  接货 送货
   */
  carriagetype: string,
  /**
   * 省
   */
  province: string,
  /**
   *  市
   */
  city: string,
  /**
   * 详细地址
   */
  detailaddress: string,
}
export interface RecommandReturnParam {
  /**
   * 区
   */
  AREA: string
  /**
   * 市
   */
  CITY: string
  /**
   * 网点名字
   */
  COMPANYNAME: string
  /**
   * 维度
   */
  LAT: string
  /**
   * 经度
   */
  LNG: string
  /**
   * 全部地址
   */
  LOCAL: string
  /**
   * 省
   */
  PROVINCE: string
  /**
   * 网点编号
   */
  branchno: string
  /**
   * 距离 单位是米
   */
  distance: number
  /**
   * 电话
   */
  tel: string
}
export interface RecommandReturnParamNew {
  /**
   * 区
   */
  AREA: string
  /**
   * 市
   */
  CITY: string
  /**
   * 网点名字
   */
  COMPANYNAME: string
  /**
   * 纬度
   */
  lat: string
  /**
   * 经度
   */
  lng: string
  /**
   * 全部地址
   */
  LOCAL: string
  /**
   * 省
   */
  PROVINCE: string
  /**
   * 网点编号
   */
  branchno: string
  /**
   * 距离 单位是米
   */
  distance: number
  /**
   * 电话
   */
  tel: string
}
//推荐网点接口  // /selfmachine-system/order/recommandBranch  process.env.NODE_ENV === 'production' http://192.168.10.32:85/assign/map/returnForm
export function recommandBranch(data: RecommandParam): Promise<RecommandReturnParam[]> {
  return new Promise<RecommandReturnParam[]>(async (resolve, reject) => {
    const res = await $get<{ code: number, data: string, msg: string }>('/selfmachine-system/order/recommandBranch', data)

    if (res.data.code == 200) {
      let arr: RecommandReturnParam[] = JSON.parse(res.data.data).branch
      resolve(arr)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}

//配送方案接口
//http://192.168.10.32:85
export function recommandBranchNew(data: any): Promise<RecommandReturnParamNew[]> {
  return new Promise<RecommandReturnParamNew[]>(async (resolve, reject) => {
    const res:any = await $get<{ code: number, data: string, msg: string }>('/selfmachine-system/branch/returnForm', data)
    if (res.data.branch) {
      let arr:any[] = res.data.branch
      resolve(arr)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}

export interface LongPriceParam {
  /**
     * 到达地 省市区 中间用空格隔开
     */
  desarea?: string,
  /**
    * 起始地 省市区 中间用空格隔开
    */
  strarea?: string,
  /**
   * 起运地网点编号
   */
  startCompany: string,
  /**
   *  日期
   */
  senddate: string,
  /**
   * 目的地网点编号
   */
  descCompany: string,
  /**
   * 承运类型
   */
  type: string,
  /**
   *  货物体积
   */
  volume: number,
  /**
   *  货物重量
   */
  weight: number,
}
//长途运费
export function getLongPrice(data: LongPriceParam): Promise<any> {
  return new Promise<any>(async (resolve, reject) => {
    const res = await $post<{ code: number, data: string, msg: string }>('/selfmachine-system/order/longPrice', data)

    if (res.data.code == 200) {


      resolve(res.data.data)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}

interface CalcCargoPriceParam {
  /**
   * 订单id
   */
  orderId: any,
  /**
   * 网点编号
   */
  branch: string,
  /**
   *  后端 暂时写死
   */
  excess: boolean,
  /**
   * 是否首单
   */
  first: boolean,
  /**
   * 里程
   */
  mileage?: number,
  /**
   * 货物体积
   */
  volume: number,
  /**
   * 货物重量
   */
  weight: number,
  source?: string,
  destination?: string,
}
//接货费
export function calcCargoPrice(data: CalcCargoPriceParam): Promise<number> {
  return new Promise<number>(async (resolve, reject) => {
    const res = await $post<{ code: number, data: number, msg: string }>('/selfmachine-system/fee/calcCargoPriceNew', data)

    if (res.data.code == 200) {

      resolve(res.data.data)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}
interface CalcDeliveryPriceParam {
  /**
   * 里程
   */
  mileage: number,
  /**
   * 网点编号
   */
  branch: string, //自助机编号
  /**
   * 是否大件货物
   */
  djhw: boolean, //是否大件货物
  /**
   * 重量
   */
  weight: number,
  /**
   * 体积
   */
  volume: number,
  /**
   * 是否加急
   */
  jj: boolean, //
  /**
   * 是否限时送
   */
  xss: boolean, //
  /**
   * 是否特派区域
   */
  special: boolean,
  /**
   * 送商超
   */
  scs: boolean,
  /**
   * 进仓
   */
  jc: boolean,
  /**
   * 报关
   */
  bg: boolean,

  numbers: number,
  /**
   * 是否快件
   */
  sendTypeSep: boolean, //
  /**
   * 是否续重
   */
  excess: boolean, //
  /**
   * 是否首单
   */
  first: boolean,
}
//送货费
export function calcDeliveryPrice(data: CalcDeliveryPriceParam): Promise<number> {
  return new Promise<number>(async (resolve, reject) => {
    const res = await $post<{ code: number, data: number, msg: string }>('/selfmachine-system/fee/calcDeliveryPrice', data)
    console.log(res.data, '66');

    if (res.data.code == 200) {

      resolve(res.data.data)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}

interface CalcDischargePriceParam {
  /**
   * 货物类型（0标准，1非标，2特殊）
   */
  cargoType: string,
  /**
   * 是否电梯
   */
  isElevator: boolean,
  /**
   * 是否要上楼
   */
  isUpStairs: boolean,
  /**
   * 货物体积
   */
  volume: number,
  /**
   * 货物重量
   */
  weight: number,
  /**
   * 楼层
   */
  floor:string,
}
//卸货费
export function calcDischargePrice(data: CalcDischargePriceParam): Promise<number> {
  return new Promise<number>(async (resolve, reject) => {
    const res = await $post<{ code: number, data: number, msg: string }>('/selfmachine-system/fee/calcDischargePrice', data)
    console.log(res.data, '66');

    if (res.data.code == 200) {

      resolve(res.data.data)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}



export interface OrderParam {
  /**
   * 附件图片
   */
  filePic: string,
  /**
   * 货物类型
   */
  goodsType: number,
  /**
   *  发货人名字
   */
  shipper: string,
  /**
   * 发货人电话
   */
  shipperMobile: string,
  /**
   * 签单单位
   */
  billCompany: string,
  /**
   * 结算方式(1现付、2提付、3签单)
   */
  clearingForm: string,
  /**
   * 接货时间段
   */
  receivingTime: string,
  /**
   * 接货地址
   */
  toFromAdd: string,
  /**
   * 送货经度
   */
  fromLongitude: string,
  /**
   * 送货纬度
   */
  fromLatitude: string,
  /**
   * 接货经度
   */
  toLongitude: string,
  /**
   * 接货纬度
   */
  toLatitude: string,
  /**
   * 是否接货 1接货 0不接
   */
  isReceiving: string,
  /**
   *  收货人
   */
  receiver: string,
  /**
   *  收货人联系方式
   */
  receiverMobile: string,
  /**
   * 提货方式(自提、送货、直送)
   */
  pickUpMode: string,
  /**
   * 送货地址
   */
  fromAdd: string,
  /**
   * 配送时段
   */
  deliveryTime: string,
  /**
   * 送货类型 1、送商超 2、进仓 3、报关 4、送货补贴
   */
  deliveryType: string,
  /**
   * 承运类型
   */
  sendType: string,
  /**
   * 是否加急
   */
  urgent: number,
  /**
   * 是否包装
   */
  packing: boolean,
  /**
   * 是否卸货
   */
  dischargeCargo: string,
  /**
   * 是否楼梯
   */
  dischargeElevator: string,
  /**
   * 几层
   */
  elevatorNum: string,
  /**
   * 是否保价
   */
  insuredPrice: string,
  /**
   * 保价金额
   */
  insuredMoney: string,
  /**
   *  保费
   */
  insuredFee: number,
  /**
   * 单据服务
   */
  billServer: string,
  /**
   *  是否代收货款
   */
  collectionPayment: string,
  /**
   * 代收金额
   */
  collectionMoney: string,
  /**
   * 返还周期
   */
  returnCycle: number,
  /**
   *  服务费
   */
  serveFee: number,
  /**
   * 返还方式
   */
  returnType: number,
  /**
   * 银行卡账号
   */
  returunAccount: string,
  /**
   * 签几联
   */
  billNum: string,
  /**
   * 自助机网点编号
   */
  startBranch: string,
  /**
   * 收货人地址最近网点编号
   */
  destinationBranch: string,
}
// goodsInfo类型
export interface GoodsInfoParam {
  /**
   * 创建时间
   */
  creaTetime: string,
  /**
   * 货物名称
   */
  goodsName: string,
  /**
   * 货物件数
   */
  goodsNumber: number,
  /**
   * 货物类型
   */
  goodsType: string,
  /**
   * 货物体积
   */
  goodsVolume: number,
  /**
   * 删除标识 0、否 1、是
   */
  isDel: string,
  /**
   * 订单编号
   */
  orderId: string,
  /**
   * 包装
   */
  packing: string,
  /**
   * 规格
   */
  specifications: string,
  /**
   * id
   */
  ssogId: string,
  /**
   * 已提件数
   */
  upNumber: string,
  /**
   * 更新时间
   */
  updateTime: string,
  /**
   * 重量
   */
  weigh: number,
}
// costList类型
export interface CostListParam {
  /**
   * 费用实际金额
   */
  actualMoney: number,
  /**
   * 承运单号
   */
  billNo: string,
  /**
   * 费用类型 订单通用信息表配置 ID
   */
  costType: string,
  /**
   * 创建时间
   */
  creaTetime: string,
  /**
   * 费用金额
   */
  money: number,
  /**
   * 是否已付款 0否 1是
   */
  paid: string,
  /**
   * 已付金额
   */
  paidMoney: string,
  /**
   * 付款类型 1、付款 2、退款
   */
  payType: string,
  /**
   * 付款时间
   */
  paymanyTime: string,
  /**
   * 关联订单主键
   */
  ssoId: string,
  /**
   * ID
   */
  ssocId: string,
  /**
   * 具体费用名称
   */
  ssopRemark: string,
  /**
   * 费用类型名称
   */
  ssopType: string,
  /**
   * 更新时间
   */
  updateTime: string,
}
export interface OrderOrderParam {
  /**
  * 订单信息
  */
  order: OrderParam,
  /**
   * 商品信息
   */
  goodsInfo: GoodsInfoParam[],
  /**
   * 支付信息
   */
  costList: CostListParam[],
}
interface OderReturnParams{
  /**
   * id
   */
  orderId:string
  /**
   * 发货码
   */
  sendCode:number
  /**
   * 1 是成功
   */
  isSuccess:string
}
//发货提交
// /order/order /workerOrder/workerConfirmOrder
export function orderOrder(data: any): Promise<OderReturnParams> {
  return new Promise<OderReturnParams>(async (resolve, reject) => {
    const res = await $post<{ code: number, data: OderReturnParams, msg: string }>('/selfmachine-system/order/order', data)
    if (res.data.code == 200) {
      resolve(res.data.data)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}
interface OrderListParams{
  /**
   * 下单成功返回的  查询该id的单子
   */
  ssoId:string
}


export function orderList(data: OrderListParams,pageNum:number,pageSize:number): Promise<OderReturnParams> {
  return new Promise<OderReturnParams>(async (resolve, reject) => {
    const res = await $post<{ code: number, rows: any, msg: string,total:number }>(`/selfmachine-system/order/list/reservation?pageNum=${pageNum}&pageSize=${pageSize}`, data)
    if (res.data.code == 200) {

      resolve(res.data.rows)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}

export function changeList(data: OrderListParams,pageNum:number,pageSize:number): Promise<OderReturnParams> {///selfmachine-system/order/list/modify
  return new Promise<any>(async (resolve, reject) => {
    const res = await $post<{ code: number, rows: any, msg: string,total:number }>(`/selfmachine-system/order/list/modify?pageNum=${pageNum}&pageSize=${pageSize}`, data)
    if (res.data.code == 200) {
      resolve(res)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}



export function getPoorder(data: any): Promise<DataParams[]> {
  return new Promise<DataParams[]>(async (resolve, reject) => {
    const res = await $get<{ code: number, data: DataParams[], msg: string }>('/myOrders/getpoorder', data)
    if (res.data.code == 200) {
      resolve(res.data.data)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}

//我要查件
export function getPoorderByBillNo(data: any): Promise<DataParams[]> {
  return new Promise<any>(async (resolve, reject) => {
    const res = await $post<{ code: number, data: DataParams[], msg: string }>('/selfmachine-system/logistics/getLogisticsInfo', data)
    if (res.data.code == 200) {
      resolve(res.data)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}

//查询网点电话
export function getBranchTel(data: any[]) {
  return new Promise<any>(async (resolve, reject) => {
    const res = await $post<{ code: number, data: DataParams[], msg: string }>('/selfmachine-system/branch/branchTel', data)
    if (res.data.code == 200) {
      resolve(res.data)
    } else {
      Taro.showToast({
        icon: 'error',
        title: res.data.msg
      })
      reject(res.data.msg)
    }
  })
}

export function noUserToLogin() {
  if(!Taro.getStorageSync('userToken')){
    Taro.navigateTo({
      url: `/pages/login/index`
    })
  }
}
export function checkLogin() {
  if(!!Taro.getStorageSync('userToken')){
    state.setOpenid(Taro.getStorageSync('openid'))
    state.setToken(Taro.getStorageSync('userToken'))
    state.setUserInfo(JSON.parse(Taro.getStorageSync('userInfo')||'{}'))
  }
}


//合并工作人员端的
export async function $delete<TData = any>(urlKey: string, data?: TData) {
  let url;
  if (urlKey.includes("http://") || urlKey.includes("https://")) {
    url = urlKey;
  } else {
    url = shBaseUrl + urlKey;
  }
  return await Taro.request({
    method: "DELETE",
    header: {
      "content-type": "application/json",
      Authorization: state.token,
    },
    url: url,
    data: data,
  });
}
